import requests
import json
import time

webhook_url = '''https://oapi.dingtalk.com/robot/send?access_token=0d9301bda108810192326ee3cdcae0ccc94f8733ef7e04c06ceefb40881d426b'''

def getStatus(hostname):
    url = '''https://pregateway.ibim.com.cn/quotaProApi/project/api/rpa/Calc/queryStatus?hostName=''' + hostname

    response = requests.get(url=url)
    return response

def send_message_robot(content):
    params = {
        'msgtype': 'text',
        'text' : {
            'content' : content
        }
    }
    header = {
        'Content-Type': 'application/json'
    }
    requests.post(url = webhook_url, json=params, headers=header)

def check_machine_status(hostname):
    response = getStatus(hostname)
    dict = json.loads(response.text)
    flag = False
    # print(time.time())
    # print(dict[0]['exportStartTime'])
    # print(time.time() - dict[0]['exportStartTime'])
    for i in range(7) :
        if i >= len(dict) :
            continue
        DTO = dict[i]
        # print(DTO)
        status = DTO['status']
        # print(status)
        # print('---')
        if status == 1:
            flag = True
    return flag

def main_process(hostname):
    flag = check_machine_status(hostname=hostname)
    # print(flag)
    if not flag:
        send_message_robot("rpa机器" + hostname + "::连续失败五次")

# machine_A = 'COMP-755-A2834'
machine_B = 'COMP-755-A2216'
machine_C = 'COMP-755-A2217'
machine_D = 'VSERV-755-A1013'
count = 0
while (True):
    count = count + 1
    print(count)
    # main_process(machine_A)
    main_process(machine_B)
    main_process(machine_C)
    main_process(machine_D)
    time.sleep(600)
